package jmine.tec.hydra.services.alterar;

import jmine.tec.di.annotation.Injected;
import jmine.tec.hydra.domain.senderRegistry.MQSenderRegistry;
import jmine.tec.services.annotations.Execution;
import jmine.tec.services.annotations.Input;
import jmine.tec.services.annotations.Output;
import jmine.tec.services.annotations.ServiceImplementor;
import jmine.tec.services.api.ActionsEnum;
import jmine.tec.services.api.ServiceProperties;
import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.persist.dao.BeanNotFoundException;

/**
 * Serviço de alteração de um SenderRegistry que envia mensagens via JMS
 * 
 * @author lundberg
 */
@ServiceImplementor(action = ActionsEnum.ALTERAR)
public class MQSenderRegistryService {

    public static final String IDENTIFICADOR = "Identificador";

    public static final String MNEMONICO = "Mnemônico";

    public static final String QUEUE = "Fila";

    public static final String QUEUE_CONNECTION_FACTORY = "Fabrica de Conexões";

    // Injected
    private BaseDAO<MQSenderRegistry> dao;

    private ServiceProperties serviceProperties;

    // Obrigatórios
    private String mnemonico;

    /**
     * Registra um novo sender para JMS.
     * 
     * @return MQSenderRegistry
     * @throws BeanNotFoundException caso não encontre mqSenderRegistry com o mnemonico passado.
     */
    @Execution
    @Output(propertyName = IDENTIFICADOR)
    public MQSenderRegistry execute() throws BeanNotFoundException {
        MQSenderRegistry registry = this.dao.findByNaturalKey(this.mnemonico);
        registry.setQueue(this.serviceProperties.getRaw(QUEUE));
        registry.setQueueConnectionFactory(this.serviceProperties.getRaw(QUEUE_CONNECTION_FACTORY));
        registry.getPersister().save();
        return registry;
    }

    /**
     * @param dao the dao to set
     */
    @Injected
    public void setDao(BaseDAO<MQSenderRegistry> dao) {
        this.dao = dao;
    }

    /**
     * @param serviceProperties the serviceProperties to set
     */
    @Injected
    public void setServiceProperties(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    /**
     * @param mnemonico the mnemonico to set
     */
    @Input(fieldName = MNEMONICO)
    public void setMnemonico(String mnemonico) {
        this.mnemonico = mnemonico;
    }

    /**
     * @param queue the queue to set
     */
    @Input(fieldName = QUEUE)
    public void setQueue(String queue) {
    }

    /**
     * @param queueConnectionFactory the queueConnectionFactory to set
     */
    @Input(fieldName = QUEUE_CONNECTION_FACTORY)
    public void setQueueConnectionFactory(String queueConnectionFactory) {
    }
}
